# !/usr/bin/env python
# encoding: utf-8
'''
  @author: HJW
  @license: (C) Copyright 2013-2017, Node Supply Chain Manager Corporation Limited.
  @contact: hejunwang01@126.com
  @file: 模拟登录cookie.py
  @time: 2022/8/28 0028 下午 17:03
  @desc:  请求超级鹰平台
  '''


import requests
from lxml import etree
from chaojiying import Chaojiying_Client

url ="http://www.chaojiying.com/user/login/"
headers = {
        "User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.71 Safari/537.36"
    }
session = requests.session()
# 获取html页面的请求
page_next = session.get(url,headers =headers).text
#解析获取到img的url
img_url = etree.HTML(page_next)
img_url = "http://www.chaojiying.com"+img_url.xpath('/html/body/div[3]/div/div[3]/div[1]/form/div/img/@src')[0]
print(img_url)

# 下载img,保存到img文件夹中
img_data = session.get(url=img_url,headers=headers).content
with open('./img/img.png','wb') as fp:
    fp.write(img_data)

# 使用打码获取到图片的内容 ,导入超级鹰
cjy =Chaojiying_Client('13726412604', '123456', '938310')
im = open('./img/img.png', 'rb').read()						#本地图片文件路径 来替换 a.jpg 有时WIN系统须要//
# 获取到打码的字符串
codestr=cjy.PostPic(im, 1004)['pic_str']
print(codestr)

# 要传进去的参数
data ={
"user": "13726412604",
"pass": "123456",
"imgtxt": codestr,
"act": 1
}
res = session.post(url,headers =headers,data=data).text
# etree解析
tree = etree.HTML(res)

scored = tree.xpath("/html/body/div[3]/div[2]/div[1]/div[1]/span/text()")[0]

print(scored)

li_list= tree.xpath('/html/body/div[3]/div[1]/ul/li')
# 获取左侧栏的a标签内容
for li in li_list:
    a_name=li.xpath('./a/text()')[0]
    a_url="http://www.chaojiying.com/"+li.xpath('./a/@href')[0]
    print(a_name,a_url)

